/*
 * Query.java
 *
 * Created on 6 de julio de 2007, 19:03
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package db;

import java.util.Vector;

/*      
        // ejemplos query
        rows.clear();
        Vector<String> tables = new Vector<String>();
        Vector<String> values = new Vector<String>();
        Vector<String[]> items = new Vector<String[]>();
        
        rows.add( "a.lala" );
        rows.add( "b.lele" );
        rows.add( "c.lolo" );
        
        tables.add( "sasa a" );
        tables.add( "sese b" );
        tables.add( "soso c" );
        
        values.add( "'valor'" );
        values.add( "1" );
        values.add( "true" );
        
        items.add( Query.joinStrings( "row1", "2" ) );
        items.add( Query.joinStrings( "row2", "'sarasa'" ) );
        items.add( Query.joinStrings( "row3", "false" ) );
        
        System.out.println( Query.select( tables, rows, "a.id_sarasa = 1" ) );
        System.out.println( Query.insert("tabla", items) );
        System.out.println( Query.delete("tabla", "id_tabla = 7") );
        System.out.println( Query.update("tabla", items, "id_tabla = 7") );
 */

public class Query {
    private Query() {
    }
    
    public static String select( String table, String where )
    {
        StringBuffer s = new StringBuffer();
        boolean first = true;
        
        s.append( "select * from " + table );
        
        if( where != "" )
        {
            s.append( " where " + where );
        }
        
        return s.toString();
    }
    
    public static String select( String table, Vector<String> rows, String where )
    {
        StringBuffer s = new StringBuffer();
        boolean first = true;
        s.append( "select " );
        
        for( int i = 0; i < rows.size(); i++ )
        {
            if( !first )
            {
                s.append( ", " );
            }
            
            s.append( rows.get(i) );
            
            first = false;
        }
        
        s.append( " from " + table + " " );
        
        if( where != "" )
        {
            s.append( " where " + where );
        }
        
        return s.toString();
    }
    
    public static String select( Vector<String> tables, Vector<String> rows, String where )
    {
        StringBuffer s = new StringBuffer();
        boolean first = true;
        s.append( "select " );
        
        for( int i = 0; i < rows.size(); i++ )
        {
            if( !first )
            {
                s.append( ", " );
            }
            
            s.append( rows.get(i) );
            
            first = false;
        }
        
        s.append( " from " );
        
        first = true;
        
        for( int i = 0; i < tables.size(); i++ )
        {
            if( !first )
            {
                s.append( ", " );
            }
            
            s.append( tables.get(i) );
            
            first = false;
        }
        
        if( where != "" )
        {
            s.append( " where " + where );
        }
        
        return s.toString();
    }
    
    public static String insert( String table, Vector<String[]> items )
    {
        StringBuffer s = new StringBuffer();
        boolean first = true;
        s.append( "insert into " + table + "( " );
        
        for( int i = 0; i < items.size(); i++ )
        {
            if( !first )
            {
                s.append( ", " );
            }
            
            s.append( items.get(i)[ 0 ] );
            
            first = false;
        }
        
        s.append( " ) values( " );
        
        first = true;
        
        for( int i = 0; i < items.size(); i++ )
        {
            if( !first )
            {
                s.append( ", " );
            }
            
            s.append( items.get(i)[ 1 ] );
            
            first = false;
        }
        
        s.append( " )" );
        return s.toString();
    }
        
    public static String update( String table, Vector<String[]> items, String where )
    {
        StringBuffer s = new StringBuffer();
        boolean first = true;
        s.append( "update table " + table + " set " );
        
        for( int i = 0; i < items.size(); i++ )
        {
            if( !first )
            {
                s.append( ", " );
            }
            
            s.append( items.get(i)[ 0 ] + " = " + items.get( i )[ 1 ] );
            
            first = false;
        }
        
        s.append( " where " + where );
        return s.toString();
    }
    
    public static String delete( String table, String where )
    {
        return "delete from " + table + " where " + where;
    }
    
    public static String[] joinStrings( String row, String value )
    {
        String []result = new String[2];
        
        result[ 0 ] = row;
        result[ 1 ] = value;
        
        return result;
    }
}
